<?php

namespace app\models;

use Yii;
use app\services\LogService as Log;

/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $name
 * @property string $password
 * @property integer $status
 * @property string $contact_name
 * @property string $mobile
 * @property string $phone
 * @property string $fax
 * @property string $qq
 * @property string $email
 * @property integer $archived
 * @property integer $is_root
 * @property integer $user_type
 * @property string $last_update
 */
class BaseAR extends \yii\db\ActiveRecord
{   	
    public function beforeUpdate() {
        if(parent::beforeUpdate()) {
            return true;
        }
        return false;
    }

	//before save called after validation
    public function beforeSave($insert) {	
		if ($insert) {
		    $attributes = $this->attributes;
			if (in_array('creator', array_keys($attributes))) {
				$this->creator = $_SESSION['user_id'];
			}
			if (in_array('creator_name', array_keys($attributes))) {
				$this->creator_name = $_SESSION['user_name'];
			}
		} 
		return parent::beforeSave($insert);
    }

    /**
     * return void
     */
    public function afterSave($isNew, $changedOldAttributes) {
        parent::afterSave($isNew, $changedOldAttributes);
        {
            $log = new Log();
            $attributes = $this->attributes;
            $log->logUp($this->getModel(), $attributes, $changedOldAttributes, $isNew); 
        }
    }

    public function getModel() {
        $nsModel = get_called_class();
        $nsModel = explode('\\', $nsModel);
        $model = array_pop($nsModel);
        return $model;
    }

    public function beforeDelete() {
        if(parent::beforeDelete()) {
            return true;
        }
        return false;
    }
}// end of BaseAR
